package com.chinairi.mall.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 图片工具类
 * @author
 */
public class ImageUtil {
    public static BufferedImage getImageFromCode(String textCode, Integer width, Integer height, Integer interLine, Boolean randomLocation){
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        //随机操作对象
        Random r = new Random();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,width,height);
        if (interLine > 0) {
            int x = r.nextInt(4), y = 0;
            int x1 = width - r.nextInt(4), y1 = 0;
            for (int i = 0; i < interLine; i++) {
                g.setColor(Color.BLACK);
                y = r.nextInt(height - r.nextInt(4));
                y1 = r.nextInt(height - r.nextInt(4));
                g.drawLine(x, y, x1, y1);
            }
        }

        //字体大小为图片高度的80%
        int fsize=(int)(height*0.8);
        int fx=0;
        int fy=fsize;
        g.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,fsize));
        //写字符
        for(int i=0;i<textCode.length();i++){
            //每个字符高低是否随机
            fy=randomLocation?(int) ((Math.random()*0.3+0.6)*height):fy;
            g.setColor(Color.BLACK);
            g.drawString(textCode.charAt(i)+"",fx,fy);
            //依据宽度浮动
            fx+=(width / textCode.length()) * (Math.random() * 0.3 + 0.8);
        }

        //扭曲图片
        shearX(g, width, height, Color.WHITE);
        shearY(g, width, height, Color.WHITE);

        // 噪声率
        float yawpRate = 0.05f;
        //噪点数量
        int area = (int) (yawpRate * width * height);
        for (int i = 0; i < area; i++) {
            int xxx = r.nextInt(width);
            int yyy = r.nextInt(height);
            int rgb = Color.cyan.getRGB();
            image.setRGB(xxx, yyy, rgb);
        }
        g.dispose();
        return image;
    }
    private static void shearX(Graphics g, int w1, int h1, Color color) {
        Random random=new Random();
        int period = 2;

        boolean borderGap = true;
        int frames = 1;
        int phase = random.nextInt(2);

        for (int i = 0; i < h1; i++) {
            double d = (double) (period >> 1)* Math.sin((double) i / (double) period + (2.2831853071795862D * (double) phase)/ (double) frames);
            g.copyArea(0, i, w1, 1, (int) d, 0);
            if (borderGap) {
                g.setColor(color);
                g.drawLine((int) d, i, 0, i);
                g.drawLine((int) d + w1, i, w1, i);
            }
        }

    }

    private static void shearY(Graphics g, int w1, int h1, Color color) {
        Random random=new Random();
        // 50;
        int period = random.nextInt(40) + 10;

        boolean borderGap = true;
        int frames = 20;
        int phase = random.nextInt(2);
        for (int i = 0; i < w1; i++) {
            double d = (double) (period >> 1)
                    * Math.sin((double) i / (double) period
                    + (2.2831853071795862D * (double) phase)/ (double) frames);
            g.copyArea(i, 0, 1, h1, 0, (int) d);
            if (borderGap) {
                g.setColor(color);
                g.drawLine(i, (int) d, i, 0);
                g.drawLine(i, (int) d + h1, i, h1);
            }

        }
    }
}
